import numpy as np
import matplotlib.pyplot as plt

# 中文显示
plt.rcParams['font.sans-serif'] = ['SimHei', 'DejaVu Sans']
plt.rcParams['axes.unicode_minus'] = False

# 定义函数
def f(x):
    return np.sin(1/x)

# 创建两个趋近于0的不同数列
n_vals = np.arange(1, 100)

# 数列1: x_n = 1/(nπ)
x_n1 = 1/(n_vals * np.pi)
f_xn1 = f(x_n1)

# 数列2: x_n = 1/((2n+0.5)π)
x_n2 = 1/((2*n_vals + 0.5) * np.pi)
f_xn2 = f(x_n2)

# 可视化
plt.figure(figsize=(15, 5))

plt.subplot(1, 3, 1)
x_dense = np.linspace(-0.2, 0.2, 5000)
x_dense = x_dense[x_dense != 0]
y_dense = f(x_dense)
plt.plot(x_dense, y_dense, 'b-', linewidth=1, label=r'$f(x) = \sin(1/x)$')
plt.xlabel('x')
plt.ylabel('f(x)')
plt.title('函数图像')
plt.grid(True, alpha=0.3)

plt.subplot(1, 3, 2)
plt.plot(n_vals, f_xn1, 'ro-', markersize=3, linewidth=1)
plt.xlabel('n')
plt.ylabel('f(x_n)')
plt.title(r'数列1: $x_n = \frac{1}{n\pi}$')
plt.grid(True, alpha=0.3)

plt.subplot(1, 3, 3)
plt.plot(n_vals, f_xn2, 'go-', markersize=3, linewidth=1)
plt.xlabel('n')
plt.ylabel('f(x_n)')
plt.title(r'数列2: $x_n = \frac{1}{(2n+0.5)\pi}$')
plt.grid(True, alpha=0.3)

plt.tight_layout()
plt.show()

# 检查极限是否一致
print(f"数列1的极限: {f_xn1[-10:].mean():.3f}")
print(f"数列2的极限: {f_xn2[-10:].mean():.3f}")